﻿using Volo.Abp.DependencyInjection;
using YAPACS.BusinessObjects.Exams;
using YAPACS.RisServer.Abstractions;
using YAPACS.RisServer.Workflows.Register;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Technician;

public class Pass : WorkflowBase, IPass, ITransientDependency
{
    private readonly ISetBottom _setBottom;
    private readonly IExamEventService _examEventService;

    public Pass(ISetBottom setBottom, IExamEventService examEventService)
    {
        _setBottom = setBottom;
        _examEventService = examEventService;
    }

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        const string key = "Exam";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is Exam exam)
        {
            if (exam.ExamQueuing.Status == ExamQueuingStatus.Ready)
            {
                exam.ExamQueuing.Status = ExamQueuingStatus.Wait;
                exam.ExamQueuing.IsSkip = true;
                exam.ExamQueuing.SkipTimes++;
                //顺序要调整到最后
                await _examEventService.AddExamEventAsync(exam, "过号检查");
                var result = await _setBottom.InvokeAsync(new WorkflowInput { { "Exam", exam } });
                return result;
            }
            else
            {
                return new WorkflowOutput()
                {
                    Success = false,
                    Error = "检查未处于呼叫状态，不能过号"
                };
            }
        }
        return new WorkflowOutput()
        {
            Success = false,
            Error = "未找到Exam对象"
        };
    }
}
